﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.IO;
using System.Net;
using System.Net.Mime;
using System.Threading;

namespace EmaillSend
{
    public class SendParam
    {
        private MailContent mailcontent;

        public MailContent Mailcontent
        {
            get { return mailcontent; }
            set { mailcontent = value; }
        }
        private string mailTo;

        public string MailTo
        {
            get { return mailTo; }
            set { mailTo = value; }
        }

        private string id;

        public string Id
        {
            get { return id; }
            set { id = value; }
        }
        
        public SendParam(MailContent content, string to, string guid)
        {
            this.mailcontent = content;
            this.mailTo = to;
            this.id = guid;
        }
    }
    
    public class MailServer
    {
        public string id;
        public string servername;
        public int port;
        public string mailaddress;
        public string account;
        public string password;
        public string showname;
        public string backaddress;
        public int type;
        private SmtpClient smtpClient;
        public List<SendParam> sendList = new List<SendParam>();


        public SmtpClient getClient()
        {
            if (smtpClient == null)
            {
                this.BuildClient();
            }
            return smtpClient;
        }

        public void addSendParam(SendParam param)
        {
            this.sendList.Add(param);
        }

        private void BuildClient()
        {
            smtpClient = new SmtpClient(this.servername, this.port);
            smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtpClient.Credentials = new NetworkCredential(this.account, this.password);
            if (this.type == 1)
            {
                //smtpClient.UseDefaultCredentials = true;
                smtpClient.EnableSsl = true;
            }
            //smtpClient.UseDefaultCredentials = false;
            //安全连接设置
            //smtpClient.EnableSsl = false;

            smtpClient.Timeout = 120000;
        }

        public void Disponse()
        {
            if (smtpClient != null)
            {
                try
                {
                    this.smtpClient.Dispose();
                }
                catch (Exception e) { }
            }
        }

        public void SendMailMessage(MailMessage message)
        {
            if (smtpClient == null)
            {
                this.BuildClient();
            }
            
            this.smtpClient.Send(message);
        }

    }
}
